package leetcode;

/**
 * 石头碰撞，相当于尽量分为质量接近的两堆，和等和子集一样
 */
public class lc_1049 {
    public static void main(String[] args) {
        System.out.println(lastStoneWeightII(new int[]{2,7,4,1,8,1}));
    }
    public static int lastStoneWeightII(int[] stones) {
        int n = stones.length;
        int sum = 0;
        for (int k: stones) {
            sum += k;
        }
        int target = sum / 2;
        int[] dp = new int[target + 1];
        for (int i = 0; i < n ; i++) {
            for (int j = target; j >= stones[i]; j--) {
                dp[j] = Math.max(dp[j], dp[j - stones[i]] + stones[i]);
            }
        }
        return sum - dp[target] - dp[target];
    }
}
